package cn.jane.rpc.test.server;

import cn.jane.rpc.api.NameService;
import cn.jane.rpc.api.RpcAccessPoint;
import cn.jane.rpc.api.spi.ServiceSupport;
import cn.jane.rpc.test.rpc.TestRpc;
import cn.jane.rpc.test.server.rpc.TestRpcImpl;
import cn.jane.rpc.test.util.NameServiceUtil;
import java.io.Closeable;
import java.net.URI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用于测试的服务端
 */
public class Server {

    private static final Logger log = LoggerFactory.getLogger(Server.class);

    public static void main(String[] args) throws Exception {
        String serviceName = TestRpc.class.getName();
        URI nameServiceURI = NameServiceUtil.getNameServiceURI();

        TestRpc testRpc = new TestRpcImpl();

        log.info("create and start RpcAccessPoint...");
        try (RpcAccessPoint rpcAccessPoint = ServiceSupport.load(RpcAccessPoint.class);
                Closeable ignored = rpcAccessPoint.startServer()) {
            NameService nameService = rpcAccessPoint.getNameService(nameServiceURI);
            assert nameService != null;

            log.info("register service {} to RpcAccessPoint", serviceName);
            URI uri = rpcAccessPoint.addServiceProvider(testRpc, TestRpc.class);

            log.info("register service {} to NameService {}", serviceName, nameServiceURI);
            nameService.registerService(serviceName, uri);

            log.info("start to provider service {}, press enter to exit", serviceName);
            // noinspection ResultOfMethodCallIgnored
            System.in.read();
            log.info("bye");
        }
    }
}
